﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;

namespace SBLib.Common.CsvKS
{
    public abstract class CsvBase
    {
        string _fileName = string.Empty;
        string _delimiter = string.Empty;

        protected StreamWriter _swFile;

        public CsvBase(string FileName, string Delimiter)
        {
            _fileName = FileName;
            _delimiter = Delimiter;
        }

        protected abstract void ProcessObjectForWriting();
        protected abstract void ProcessObjectForWriting<T>(List<T> CustomObjectCollection);

        public virtual string WriteToFile()
        {
            try
            {
                // Open output stream
                using (_swFile = new StreamWriter(_fileName))
                {
                    ProcessObjectForWriting();
                }
                return "";

            }
            catch (Exception ex)
            {
                return ex.Message;
            }


        }

        public virtual string WriteToFile<T>(List<T> CustomeObjectCollection)
        {
            try
            {
                // Open output stream
                using (_swFile = new StreamWriter(_fileName))
                {
                    ProcessObjectForWriting<T>(CustomeObjectCollection);
                }
                return "";

            }
            catch (Exception ex)
            {
                return ex.Message;
            }


        }


        protected string GetWriteableValueForCsv(object obj)
        {
            try
            {
                // Nullable types to blank
                if (obj == null || obj == Convert.DBNull)
                    return "";

                // if string has no ','
                if (obj.ToString().IndexOf(_delimiter) == -1)
                    return obj.ToString();

                // remove backslahes
                return "\"" + obj.ToString() + "\"";
            }
            catch (Exception)
            {

                return "";
            }

        }


        protected string ConvertSingleObjectToDelimitedString<T>(T obj, bool ShowValue)
        {
            try
            {

                var properties = typeof(T).GetProperties();

                int ctr = 0;
                string[] colData = new string[properties.Count()];
                foreach (PropertyInfo property in properties)
                {
                    object propertyValue;

                    if (ShowValue == true)
                    {
                        propertyValue = property.GetValue(obj, null);
                    }
                    else
                    {
                        propertyValue = property.Name.Trim();
                    }

                    colData[ctr] = GetWriteableValueForCsv(propertyValue);
                    ctr++;
                }
                return string.Join(_delimiter, colData);

            }
            catch (Exception)
            {

                return "";
            }
        }
    }
}
